from ast import Pass
from cProfile import label
from wsgiref.validate import validator
from flask_wtf import FlaskForm
from nbformat import ValidationError
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import Length,EqualTo,Email,DataRequired
from market.models import User

#EqualTo表示的是，验证和谁一样
#Email验证输入是否是邮箱形式
#Datarequired表示是否是空
#validator用list表示，表示可以设置多重验证
class RegisterForm(FlaskForm):
    def validate_username(self,username_to_check):
        user=User.query.filter_by(username = username_to_check.data).first()
        if user:
            raise ValidationError("Username is already exists!")
    def validaye_email(self,email_to_check):
        email_address=User.query.filter_by(email_address=email_to_check.data).first()
        if email_address:
            raise ValidationError("Email address has already existed!")

    username=StringField(label='username',validators=[Length(min=2,max=30),DataRequired()])#label就是为了指定二级标题
    email_address=StringField(label='Email Address',validators=[Email(),DataRequired()])#Field是为了在html中调用生成设计好的Field
    password1=PasswordField(label='Password',validators=[Length(min=6),DataRequired()])
    password2=PasswordField(label='Confirm password',validators=[EqualTo('password1'),DataRequired()])#只有password1和password2相同，验证才通过
    submit=SubmitField(label='Create Account')
    
class LoginForm(FlaskForm):
    username=StringField(label="Enter your username",validators=[DataRequired()])
    password=PasswordField(label="Enter your password",validators=[DataRequired()])
    submit=SubmitField(label='Sign in')


class PurchaseItemForm(FlaskForm):
    submit=SubmitField(label='Purchase Item!')

class SellItemForm(FlaskForm):
    submit=SubmitField(label="Sell Item!")
